package com.lint.engine;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import org.apache.poi.hdf.extractor.Utils;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.exception.TikaException;
import org.apache.tika.utils.ParseUtils;


/**
 * The Java part of the Java-Win32 interface for SHGetSpecialFolderPath.
 * 
 * Use the predefined CSIDL_ constants as the parameter for
 * getSpecialFolderPath(). If you need a special path that isn't defined here,
 * simply look it up in shfolder.h
 * 
 */
public class LintSystem {
	public static String SF_ALLUSERSDESKTOP    = "AllUsersDesktop";
	public static String SF_ALLUSERSSTARTMENU  = "AllUsersStartMenu";
	public static String SF_ALLUSERSPROGRAMS   = "AllUsersPrograms";
	public static String SF_ALLUSERSSTARTUP    = "AllUsersStartup";
	public static String SF_DESKTOP            = "Desktop";
	public static String SF_FAVORITES          = "Favorites";
	public static String SF_MYDOCUMENT         = "MyDocuments";
	public static String SF_PROGRAMS           = "Programs";
	public static String SF_RECENT             = "Recent";
	public static String SF_SENDTO             = "SendTo";
	public static String SF_STARTMENU          = "StartMenu";
	public static String SF_STARTUP            = "Startup";

	public static String getSpecialFolder(String folder) {
		String result = "";
		try {
			File file = File.createTempFile("realhowto",".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
				+ "wscript.echo WshShell.SpecialFolders(\"" + folder + "\")\n"            
				+ "Set WSHShell = Nothing\n";

			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
			BufferedReader input =
				new BufferedReader
				(new InputStreamReader(p.getInputStream()));
			result = input.readLine();
			input.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	public static String getDesktopPath() {
		FileSystemView fsv = FileSystemView.getFileSystemView();
		return fsv.getHomeDirectory().toString();
	}

	public static File[] listDesktopFiles() {
		FileSystemView fsv = FileSystemView.getFileSystemView();
		File desktop = new File(fsv.getHomeDirectory().toString());
		return desktop.listFiles();
	}

	//Inspects the new folder for any .mp3 files that it may contain.
	static public void readMIME(String folderName) throws IOException {

		File targetFolder = new File(folderName);
		List<String> fileList = new ArrayList<String>(Arrays.asList(targetFolder.list()));
		Iterator<String> it = fileList.iterator();
		System.out.println("reading files ..");
		while ( it.hasNext()) {
			String musicFileName = it.next();
			File f = new File(folderName + "\\"+ musicFileName);
			if(f.isDirectory()) continue;
			InputStream is = new BufferedInputStream(new FileInputStream(folderName + "\\" + musicFileName));
			String mimeType = URLConnection.guessContentTypeFromStream(is);
			if(mimeType == null) continue;
			System.out.println(mimeType + " " + musicFileName);
		}
	}
	static public void readFile(String folderName) throws IOException
	{
		File targetFolder = new File(folderName);
		List<String> fileList = new ArrayList<String>(Arrays.asList(targetFolder.list()));
		Iterator<String> it = fileList.iterator();
		System.out.println("reading files ..");
		while ( it.hasNext()) {
			File f = new File(folderName + "\\"+it.next());
			System.out.println("-----------------------------------------");
			System.out.println(f.getName());
			if(!f.isFile() || f.toString().endsWith(".lnk")) continue;
			TikaConfig tc;
			try {
				tc = TikaConfig.getDefaultConfig();
				if(tc==null) continue;
				String txt = ParseUtils.getStringContent(f, tc);
				if(txt==null) continue;
				System.out.println(txt.substring(0,Math.min(30,txt.length())));
			} catch (TikaException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		 
		}
	}

}